window.onload = function () {
    new IScroll(document.querySelector('.jd_cateLeft'), {
        scrollY: true
    });
    new IScroll(document.querySelector('.hot'), {
        scrollX: true,
        scrollY: true
    });

    $("#btn_submit").click(function () {
        formSubmit();
    });

    $("#btn_submit0").click(function () {
        form0Submit();
    });


    $.get("xianyu/keywords",
        {},
        function (data) {
            for (var i = 0; i < data.length; i++) {
                $('#keywords').append('<li class=""><button style="padding:0px;width: 100%;" onclick="window.location.href=\'/index.html?keyword='+ data[i] +'\'">' + data[i] + '</button></li>');
            }
        });

    list(getURLParameter("keyword"));

    window.setInterval(list,61000);
};

let key = '';

function getURLParameter(name) {
    return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null;
}

function list(keyword) {
    var pass = false;
    if (keyword != undefined && keyword != '') {
        key = keyword;
        pass = true;
    } else if (key != undefined && key != '') {
        keyword = key;
        pass = true;
    }

    if (pass) {
        $('#title').html(keyword);
        $('#list').empty();
        $.get("xianyu/list",
            {
                'keyword': keyword
            },
            function (data) {
                for (var i = 0; i < data.length; i++) {
                    $('#list').append(
                        '<button  style="width: 30%;" onclick="window.location.href=\''+data[i].url+'\'">' +
                        '<img src="' + data[i].pic + '" style="width: 100%;height: auto;">' +
                        '<p>' + data[i].title + '</p>' +
                        '<p>￥ ' + data[i].price + '</p>' +
                        '</button>');
                }
            });
    }
}


function add() {
    $('#addword').val('');
    $('#addModal').modal();
}

function del() {
    $("#delwords").empty();
    $.get("xianyu/keywords",
        {},
        function (data) {
            for (var i = 0; i < data.length; i++) {
                $("#delwords").append('<div class="list-group-item" onclick="choose(this);"><input type="hidden" value="' + data[i] + '">' + data[i] + '</div>');
            }
        });

    $('#delModal').modal();
}

function choose(div) {
    var name = div.getAttribute("name");
    if (name) {
        div.childNodes[0].removeAttribute("name");
        div.classList.remove("active");
    } else {
        div.childNodes[0].setAttribute("name", "keyword");
        div.classList.add("active");
    }

}

function formSubmit() {
    $("#form").ajaxSubmit(function (data) {
        $.get("xianyu/keywords",
            {},
            function (data) {
                $('#keywords').empty();
                for (var i = 0; i < data.length; i++) {
                    $('#keywords').append('<li class=""><button style="padding:0px;width: 100%;" href="/index.html?keyword='+ data[i] +'">' + data[i] + '</button></li>');
                }
            });
    });
}

function form0Submit() {
    $("#form0").ajaxSubmit(function (data) {
        if (data > 0) {
            $.get("xianyu/keywords",
                {},
                function (data) {
                    $('#keywords').empty();
                    for (var i = 0; i < data.length; i++) {
                        $('#keywords').append('<li class=""><button style="padding:0px;width: 100%;" href="/index.html?keyword='+ data[i] +'">' + data[i] + '</button></li>');
                    }
                });
            alert(keyword + ' 已添加！');
        } else {
            alert(keyword + ' 已存在！');
        }
    });
}